import 'package:flutter/material.dart';
import 'package:netease_music/models/search_song_item.dart';
import 'package:netease_music/theme/custom_colors.dart';

class BuildSong extends StatelessWidget {
  final dynamic data;
  final VoidCallback ontap;
  const BuildSong({
    super.key,
    required this.data,
    required this.ontap,
  });

  // 数据解析方法
  SearchSongItem _parseData() {
    if (data is Map) {
      return SearchSongItem(
        name: data['name'],
        artists: data['artists'],
        mv: data['mv'],
        alblum: data['alblum'],
        id: data['id'],
        picUrl: data['picUrl'],
      );
    } else {
      return const SearchSongItem(
        name: '未知歌名',
        artists: ['张杰', '谢娜'],
        mv: 0,
        alblum: '明天过后',
        id: 0,
        picUrl:
            'https://p2.music.126.net/fM7vC38wA8N_EK7zMSHfpw==/109951170529173799.jpg',
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    final song = _parseData();
    return InkWell(
      onTap: ontap,
      splashColor: Colors.grey.withOpacity(0.3), // 水波纹颜色
      highlightColor: Colors.grey.withOpacity(0.1), // 按压高亮色
      child: Container(
        margin: const EdgeInsets.symmetric(
          vertical: 0,
          horizontal: 12,
        ),
        padding: const EdgeInsets.symmetric(
          vertical: 8,
          horizontal: 0,
        ),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Theme.of(context).extension<CustomColors>()!.borderColor,
              width: 1,
            ),
          ),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Flexible(
                        child: Text(
                          song.name,
                          style: Theme.of(context).textTheme.bodyLarge,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      const SizedBox(
                        width: 4,
                      ),
                      if (song.mv != 0) ...[
                        GestureDetector(
                          onTap: () {},
                          child: Container(
                            padding: const EdgeInsets.symmetric(
                              horizontal: 4,
                              vertical: 1,
                            ),
                            decoration: BoxDecoration(
                              border: Border.all(
                                  color: Theme.of(context).primaryColor,
                                  width: 1),
                              borderRadius: BorderRadius.circular(2),
                            ),
                            child: Row(
                              children: [
                                Icon(
                                  Icons.play_arrow,
                                  color: Theme.of(context).primaryColor,
                                  size: 8,
                                ),
                                const SizedBox(width: 2),
                                Text(
                                  'MV',
                                  style: TextStyle(
                                    color: Theme.of(context).primaryColor,
                                    fontSize: 8,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ]
                    ],
                  ),
                  Row(
                    children: [
                      Flexible(
                        child: Text(
                          song.artists.map((e) => e.toString()).toString(),
                          style: TextStyle(
                            color: Theme.of(context)
                                .extension<CustomColors>()!
                                .blue,
                            fontSize: 12,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      Text(
                        ' - ',
                        style: Theme.of(context).textTheme.bodyMedium,
                      ),
                      Text(
                        song.alblum,
                        style: Theme.of(context).textTheme.bodyMedium,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ],
                  ),
                ],
              ),
            ),
            IconButton(
              onPressed: () {},
              icon: Icon(
                Icons.more_vert,
                color: Theme.of(context).extension<CustomColors>()!.borderColor,
                size: 18,
              ),
            )
          ],
        ),
      ),
    );
  }
}
